﻿Imports System.Data.SqlClient

Public Class frmClaimsManagement

    Sub FillClaims()
        Try
            If Me.txtClientName.Text.Trim.Length > 0 Then
                Me.Cursor = Cursors.WaitCursor

                Dim StrCond As String = ""

                'Check Claim Type
                If Me.RMotor.Checked = True Then
                    StrCond = " and ClaimType=N'MOT'"

                ElseIf Me.RMedical.Checked = True Then
                    StrCond = " and ClaimType=N'MED'"

                ElseIf Me.RGeneral.Checked = True Then
                    StrCond = " and ClaimType=N'GEN'"
                End If

                'Check Claim Status
                If Me.RActive.Checked = True Then
                    StrCond += " and Status=N'Active'"

                ElseIf Me.RClosed.Checked = True Then
                    StrCond += " and Status=N'Closed'"

                ElseIf Me.RSettled.Checked = True Then
                    StrCond += " and Status=N'Settled'"

                ElseIf Me.RRejected.Checked = True Then
                    StrCond += " and Status=N'Rejected'"

                ElseIf Me.RPending.Checked = True Then
                    StrCond += " and Status=N'Pending'"

                ElseIf Me.RSpecialApproval.Checked = True Then
                    StrCond += " and Status=N'Special Approval'"

                ElseIf Me.RAll.Checked = True Then
                    StrCond += ""
                Else
                    Me.GridClaims.Rows.Clear()
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If

                Dim cmd As New SqlCommand("Select SNo,ClaimNo,Status,InsuranceCompany,PolicyNo,MembName,MedIDCarNo,ClaimAmount," & _
                                          "DateOfLoss,DateOfReceive,DateOfSubmission,DateOfDeadline,IsNull(InsurCompClaimNo,N'') InsurCompClaimNo," & _
                                          "IsNull(Notes,N'') Notes,SavedUser,SavedDate From Claims Where ClientID=" & Me.txtClientNo.Text.Trim & StrCond, cnn)
                Dim Reader As SqlDataReader

                Me.GridClaims.Rows.Clear()

                cnn.Open()
                Reader = cmd.ExecuteReader
                While (Reader.Read)
                    Me.GridClaims.Rows.Add(New String() {Reader.Item("SNo"), Reader.Item("ClaimNo"), Reader.Item("Status"), _
                                                        Reader.Item("InsuranceCompany"), Reader.Item("PolicyNo"), Reader.Item("MembName"), _
                                                        Reader.Item("MedIDCarNo"), CDbl(Reader.Item("ClaimAmount")).ToString("N2"), _
                                                        Format(CDate(Reader.Item("DateOfLoss")), "yyyy/MM/dd"), _
                                                        Format(CDate(Reader.Item("DateOfReceive")), "yyyy/MM/dd"), _
                                                        Format(CDate(Reader.Item("DateOfSubmission")), "yyyy/MM/dd"), _
                                                        Format(CDate(Reader.Item("DateOfDeadline")), "yyyy/MM/dd"), _
                                                        Reader.Item("InsurCompClaimNo"), Reader.Item("Notes"), Reader.Item("SavedUser"), _
                                                        Format(CDate(Reader.Item("SavedDate")), "yyyy/MM/dd"), "Print"})
                End While
                cnn.Close()

                'Check Colors
                For Each row As DataGridViewRow In Me.GridClaims.Rows
                    If row.Cells(2).Value = "Active" Then
                        row.Cells(2).Style.BackColor = Color.Yellow

                    ElseIf row.Cells(2).Value = "Closed" Then
                        row.Cells(2).Style.BackColor = Color.Magenta

                    ElseIf row.Cells(2).Value = "Settled" Then
                        row.Cells(2).Style.BackColor = Color.GreenYellow

                    ElseIf row.Cells(2).Value = "Rejected" Then
                        row.Cells(2).Style.BackColor = Color.Red

                    ElseIf row.Cells(2).Value = "Pending" Then
                        row.Cells(2).Style.BackColor = Color.Orange

                    ElseIf row.Cells(2).Value = "Special Approval" Then
                        row.Cells(2).Style.BackColor = Color.LightSteelBlue
                    End If
                Next

                Me.Cursor = Cursors.Default
            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim a As New frmSearchCustID
        a.ShowDialog()

        If SelClientID = 0 Then
            Exit Sub
        End If

        Me.txtClientNo.Text = SelClientID
        Me.txtClientName.Text = SelClientName

        If Me.txtClientName.Text.Trim.Length > 0 Then
            FillClaims()
        End If
    End Sub

    Private Sub txtClientNo_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtClientNo.KeyUp
        If Me.txtClientNo.Text.Trim.Length > 0 Then
            If e.KeyCode = Keys.Enter Then
                Me.txtClientName.Text = GetClientName(CInt(Me.txtClientNo.Text.Trim))
                If Me.txtClientName.Text.Trim.Length > 0 Then
                    FillClaims()
                End If
            End If
        End If
    End Sub

    Private Sub txtClientNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtClientNo.TextChanged
        Me.txtClientName.Clear()
        Me.GridClaims.Rows.Clear()
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.txtClientName.Text.Trim.Length = 0 Then
            MsgBox("Please enter a valid client ID")
            Exit Sub
        Else
            Dim a As New frmClaimsNew
            a.txtClientID.Text = Me.txtClientNo.Text.Trim
            a.txtClientName.Text = Me.txtClientName.Text
            a.ShowDialog()

            FillClaims()
        End If
    End Sub

    Private Sub RPending_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RActive.CheckedChanged
        FillClaims()
    End Sub

    Private Sub RSettled_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RSettled.CheckedChanged
        FillClaims()
    End Sub

    Private Sub RRejected_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RRejected.CheckedChanged
        FillClaims()
    End Sub

    Private Sub RAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RAll.CheckedChanged
        FillClaims()
    End Sub

    Private Sub GridClaims_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridClaims.CellClick
        If e.ColumnIndex = 16 Then
            PrintClaimLetter(Me.GridClaims.Rows(e.RowIndex).Cells(1).Value)
        End If
    End Sub

    Private Sub GridClaims_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridClaims.CellDoubleClick
        If e.RowIndex <> -1 Then
            Dim a As New frmClaimsUpdate
            a.SNo = CInt(Me.GridClaims.CurrentRow.Cells(0).Value)
            a.ClaimNo = Me.GridClaims.CurrentRow.Cells(1).Value
            a.ShowDialog()

            FillClaims()
        End If
    End Sub

    Private Sub RPending_CheckedChanged_1(sender As System.Object, e As System.EventArgs) Handles RPending.CheckedChanged
        FillClaims()
    End Sub

    Private Sub RSpecialApproval_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RSpecialApproval.CheckedChanged
        FillClaims()
    End Sub

    Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub RClosed_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RClosed.CheckedChanged
        FillClaims()
    End Sub

    Private Sub RAllClaimsTypes_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RAllClaimsTypes.CheckedChanged
        FillClaims()
    End Sub

    Private Sub RGeneral_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RGeneral.CheckedChanged
        FillClaims()
    End Sub

    Private Sub RMedical_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RMedical.CheckedChanged
        FillClaims()
    End Sub

    Private Sub RMotor_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RMotor.CheckedChanged
        FillClaims()
    End Sub

    Private Sub frmClaimsManagement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
    End Sub
End Class
